// miniprogram/pages/home/home.js
const {information,simple1,simple2,shoop,categories,Banners} = require("../../data/data")
const logo =["../../images/logo/downarrow.png","../../images/logo/uparrow.png"]
const navigations = [
  {
    id:"0",
    title:"首页"
  },
  {
    id:"1",
    title:"电脑办公"
  },
  {
    id:"2",
    title:"大家电"
  },
  {
    id:"3",
    title:"小家电"
  },
  {
    id:"4",
    title:"手机"
  },
  {
    id:"5",
    title:"图书"
  },
  {
    id:"6",
    title:"食品饮料"
  },
  {
    id:"7",
    title:"运动"
  },
  {
    id:"8",
    title:"家具厨具"
  },
  {
    id:"9",
    title:"腕表珠宝"
  },
  {
    id:"10",
    title:"玩具乐器"
  },
  {
    id:"11",
    title:"母婴"
  },
  {
    id:"12",
    title:"内衣配饰"
  },
  {
    id:"13",
    title:"童装"
  },

  

];
const navigations1 = [
  {
    id:"0",
    title:"首页"
  },
  {
    id:"1",
    title:"电脑办公"
  },
  {
    id:"2",
    title:"大家电"
  },
  {
    id:"3",
    title:"小家电"
  },
  {
    id:"4",
    title:"手机"
  },
  {
    id:"5",
    title:"图书"
  },
  {
    id:"6",
    title:"食品饮料"
  },
  {
    id:"7",
    title:"运动"
  },
  {
    id:"8",
    title:"家具厨具"
  },
  {
    id:"9",
    title:"腕表珠宝"
  },
  {
    id:"10",
    title:"玩具乐器"
  },
  {
    id:"11",
    title:"母婴"
  },
  {
    id:"12",
    title:"内衣配饰"
  },
  {
    id:"13",
    title:"童装"
  },

  

];
const navigations2= ["全部分类"];
const a = 0
const navigationindex = 0;

const db = wx.cloud.database();
const shopCollection = db.collection("shop")
const app = getApp;
const {envList} = require('../../envList')
const _ = db.command
const shop = []

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop,
    navigationindex:0,
    navigations,
    Banners,
    categories,
    logoIndex:0,
    navigation1s:"",
    navigation2s:"",
    information:[],
    message:[],
    simple1,
    simple2,
    logo,
    a,
    shoop,
    navScrollLeft: 0
  },
  gotodetial(event){
    console.log(event,"-----------------");
    let {id} = event.currentTarget.dataset;
    
    wx.navigateTo({
      url:"../detail/detail?id="+id
    })
  },
   async changeType1(e){
    var singleNavWidth = this.data.windowWidth / 7;
    console.log(e);
    let tid =  e.currentTarget.dataset.id;
    console.log(tid)
    let {data} = await shopCollection
    .where({Type:_.eq(Number(tid))})
    .orderBy("S_price","desc")
    .get()
    this.setData({
    shopinformation:data,
    navigationindex:tid,
    Banners:0,
    categories:0,
    shoop:0,
    logo,
    navigations,
    navigations1s:"",
    navigations2s:"",
    information,
    simple1,
    simple2:0
    })
    if(tid ==0){
      this.setData({
      navScrollLeft: (tid-2) * singleNavWidth,
      navigationindex:tid,
      logoIndex:0,
      a:0,
      Banners,
      categories,
      shoop,
      navigation1s:"",
      navigation2s:"",
      information,
      simple1,
      simple2
      })
    }else{
    let lists = [];
    this.data.message.forEach(list=>{
      if(list.type == tid ){
        lists.push(list)  
      }
    })
    console.log(lists)
    this.data.message.filter(list=>{
      list.type == tid
    })
    this.setData({
      navScrollLeft: (tid-2) * singleNavWidth,
      logoIndex:0,
      navigation1s:"",
      navigation2s:"",
      navigationindex:tid,
      information:lists,
      a:1,
      simple2:0,
      shoop
    })
     }
  },
  downopen(e){
    let id = e.currentTarget.dataset.id;
    if(id==0){
      this.setData({
       logoIndex:1,
       navigation1s:navigations1,
       navigation2s:navigations2
    })
    }else{
      this.setData({
       logoIndex:0,
       navigation1s:"",
       navigation2s:""
      })
    }
  },

  async changeType2(e){
    console.log(e)
    var bid = e.currentTarget.dataset.id;
    console.log(bid)
    let {data} = await shopCollection
    .where({Type:_.eq(Number(bid))})
    .orderBy("S_price","desc")
    .get()
    this.setData({
      shopinformation:data,
    navigationindex:bid,
    Banners:0,
    categories:0,
    shoop:0,
    logo,
    navigation1s:"",
    navigation2s:"",
    information,
    simple1,
    simple2:0
    })
    if(bid ==0){
      this.setData({
        navigationindex:bid,
      a:0,
      Banners,
      categories,
      shoop,
      navigation1s:"",
      navigation2s:"",
      information,
      logoIndex:0,
      simple1,
      simple2
      })
    }else{
    let lists = [];
    this.data.message.forEach(list=>{
      if(list.type == bid ){
        lists.push(list)  
      }
    })
    console.log(lists)
    this.data.message.filter(list=>{
      list.type == bid
    })
    this.setData({
      navigationindex:bid,
      information:lists,
      a:1,
      simple2:0,
      shoop,
      navigation1s:"",
      logoIndex:0,

    })
     }
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(){
    // await wx.cloud.callFunction({
    //   name:"getdatabase",
    //   success:(res)=>{
    //    console.log(res.result.data);
    //   }
    // })
    let {data} = await shopCollection
    .where({Type:_.eq(Number(0))})
    .orderBy("S_id","asc")
    .get()
    this.setData({
      shopinformation:data,
      information:information,
      message:information
    })
    wx.getSystemInfo({
      success: (res) => {
          this.setData({
              pixelRatio: res.pixelRatio,
              windowHeight: res.windowHeight,
              windowWidth: res.windowWidth
          })
      },
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    setTimeout(()=>{
      wx.stopPullDownRefresh()
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})